//
//  TopSubjectView.m
//  NaRiLSVMT
//
//  Created by jmsre on 14-8-10.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//

#import "TopSubjectView.h"
#import "SVGloble.h"
#import "ScreenPreviewView.h"


//按钮空隙
#define BUTTONGAP 20
//滑条宽度
#define CONTENTSIZEX 200
//按钮id
#define BUTTONID (sender.tag-100)
//滑动id
#define BUTTONSELECTEDID (topViewSelectedChannelID - 100)


UIImageView *subjectShadowImageView = nil;   //选中阴影

extern  dispatch_source_t timer_playFlowScreen;//"流程控制"播放定时器

extern BOOL    IsPopTwice;//是否需要pop两次.
//extern  BOOL    isNodePagePlay;//是否是在节点界面播放。是：YES.否：NO.

extern  NSInteger   lastFlowNumber;//前一个处理的流程No.或者当前


NSMutableArray  *subjectButtonArr = nil;//主题两个按钮数组

@implementation TopSubjectView
@synthesize nameArray;
@synthesize topViewSelectedChannelID;


+ (TopSubjectView *)shareInstance {
    static TopSubjectView *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance=[[self alloc] initWithFrame:CGRectMake(0, 0, CONTENTSIZEX, 44)];
    });
    return _instance;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        
        userSelectedChannelID = 100;
        topViewSelectedChannelID = 100;
        
        self.buttonOriginXArray = [NSMutableArray array];
        self.buttonWithArray = [NSMutableArray array];

    }
    return self;
}


- (void)initWithNameButtons
{
    /*******************************/
    userSelectedChannelID = 100;
    topViewSelectedChannelID = 100;
    if (subjectButtonArr != nil) {
        for(UIButton *but in subjectButtonArr)
        {
            but.selected = NO;
        }
        [subjectButtonArr removeAllObjects];
    }
    else
    {
        subjectButtonArr = [NSMutableArray array];
    }
    if (self.buttonOriginXArray != nil)
    {
        [self.buttonOriginXArray removeAllObjects];
        self.buttonOriginXArray = [NSMutableArray array];
    }
    else
    {
        self.buttonOriginXArray = [NSMutableArray array];
    }
    if (self.buttonWithArray != nil)
    {
        [self.buttonWithArray removeAllObjects];
        self.buttonWithArray = [NSMutableArray array];
    }
    else
    {
        self.buttonWithArray = [NSMutableArray array];
    }
    /*******************************/
    float xPos = 5.0;
    for (int i = 0; i < [self.nameArray count]; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.backgroundColor = ClearColor;
        NSString *title = [self.nameArray objectAtIndex:i];
        
        [button setTag:i+100];
        if (i == 0) {
            button.selected = YES;
        }
        [button setTitle:title forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:20.0];
        [button setTitleColor:[SVGloble colorFromHexRGB:@"ffffff"] forState:UIControlStateNormal];
        [button setTitleColor:[SVGloble colorFromHexRGB:@"ffffff"] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(selectNameButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [subjectButtonArr addObject:button];
        
        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:20]};
        
        int buttonWidth = 0;
        if(iOS7){
            buttonWidth = [title boundingRectWithSize:CGSizeMake(100, 30) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size.width;
        }
        else{
            buttonWidth = [title sizeWithFont:button.titleLabel.font
                            constrainedToSize:CGSizeMake(100, 30)
                                lineBreakMode:NSLineBreakByClipping].width;
        }
        
        button.frame = CGRectMake(xPos, 9, buttonWidth+BUTTONGAP, 30);
        
        [_buttonOriginXArray addObject:@(xPos)];
        
        xPos += buttonWidth+BUTTONGAP;
        
        [_buttonWithArray addObject:@(button.frame.size.width)];
        
        [self addSubview:button];
    }
    
    subjectShadowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(BUTTONGAP -15, 0,100.0, 44)];
    [subjectShadowImageView setImage:[UIImage imageNamed:@"yellow_line_and_shadow.png"]];
    [self addSubview:subjectShadowImageView];
}



//点击顶部标签
- (void)selectNameButton:(UIButton *)sender
{    
    //如果更换按钮
    if (sender.tag != userSelectedChannelID) {
        //取之前的按钮
        UIButton *lastButton = (UIButton *)subjectButtonArr[userSelectedChannelID-100];
        lastButton.selected = NO;
        //赋值按钮ID
        userSelectedChannelID = sender.tag;
    }
    
    //按钮选中状态
    if (!sender.selected) {
        sender.selected = YES;
        
        [UIView animateWithDuration:0.25 animations:^{
            float x = sender.frame.origin.x;
            CGRect r = subjectShadowImageView.frame;
            r.origin.x = x;
            r.size.width = 100.0;
            subjectShadowImageView.frame = r;
           // [subjectShadowImageView setFrame:CGRectMake(sender.frame.origin.x, 0, 100.0, 44)];
            
        } completion:^(BOOL finished) {
            if (finished) {
                //赋值滑动列表选择频道ID
                topViewSelectedChannelID = sender.tag;
                
                //设置主题页出现
                if(100 == topViewSelectedChannelID)
                {
//                    for(NSDictionary *dic in allFlowDataArr)
//                    {
//                        NSEnumerator    *enuObj = [dic objectEnumerator];
//                        for(NSDictionary *dic2 in enuObj)
//                        {
//                            if([[dic2 objectForKey:@"playFlag"] intValue] == 1)
//                            {
//                                isHaveFlowPlaying = YES;
//                                if (timer_playFlowScreen != nil) {
//                                dispatch_source_cancel(timer_playFlowScreen);
//                                }
//                                allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"pauseFlag"] = @(0);
//                                allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"playFlag"] = @(0);
//                                break;
//                            }
//                            else
//                            {
//                                isHaveFlowPlaying = NO;
//                            }
//                        }
//                        if (isHaveFlowPlaying == YES)
//                        {
//                            isHaveFlowPlaying = NO;
//                            break;
//                        }
//                    }
//                    
//                    //重新发请求，重新加载一遍
//                    if(IsPopTwice == YES)
//                    {
//                        [self.delegate HandlePopTwiceViewController:self];
//                        IsPopTwice = NO;
//                        allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"isNodePagePlay"] = @(0);
//                    }
//                    else
//                    {
                        [self.delegate HandlePopViewController:self];
                  //  }
//                    [self.delegate HandleJumpToScreenPreviewView:self];
                }
                else if (101 == topViewSelectedChannelID)
                {
                    //重新发请求，重新加载一遍
                    [self.delegate HandlePushViewController:self];
                }
            }
        }];
        
    }
    //重复点击选中按钮
    else {
        
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
